import { _decorator, Component, find, instantiate, Node, Sprite, SpriteFrame, } from 'cc';
import { WsMgr } from './TCP/WsMgr';
import { Utils } from './Common/Utils';


const { ccclass, property } = _decorator;
type ItemNode = { icon: Sprite};
@ccclass('GameList20')
export class GameList20 extends Component {
    @property({ type: SpriteFrame })
    defalutSpriteFrame: SpriteFrame = null;

    @property({ type: SpriteFrame })
    redSpriteFrame: SpriteFrame = null;

    @property({ type: SpriteFrame })
    blackSpriteFrame: SpriteFrame = null;

    @property({ type: Node })
    startNode: Node = null;

    @property({ type: Node })
    GameList: Node = null;

    private ItemNodes: ItemNode[] = [];
   

    start() {
   
         
        for (let i = 0; i < 20; i++) {
            let node: Node;
            node = instantiate(this.startNode);
            this.GameList.addChild(node);
            this.ItemNodes.push({ icon: node.getComponent(Sprite)});
            const x=this.startNode.position.x+53*(i%10);
            const y=this.startNode.position.y-40*(Math.floor(i/10));
            this.ItemNodes[i].icon.spriteFrame=this.defalutSpriteFrame;
            node.setPosition(x,y)
        }

    }

    update(deltaTime: number) {
        
    }

    onLoad() {
        
        console.log('onLoad');
        
    }
    updateList() {
       let raw =WsMgr.getInstance().getGameList();
       let gameList=Utils.deepCopy(raw);
       let  length=raw.length;
        if (length>20) {
            let saveCounts=length%10==0?20:10+length%10;//主页最多显示20个
            gameList= gameList.slice(gameList.length-saveCounts);
            console.log('slice=',gameList);
        }
       

        for (let i = 0; i < this.ItemNodes.length; i++) {
            this.ItemNodes[i].icon.spriteFrame=this.defalutSpriteFrame;
            this.ItemNodes[i].icon.node.getChildByName('winFlag').active=false;
        }

        for (let i = 0; i < gameList.length; i++) {
         if (gameList[i].item==1) {
            this.ItemNodes[i].icon.spriteFrame=this.redSpriteFrame;
         }else{
            this.ItemNodes[i].icon.spriteFrame=this.blackSpriteFrame;
         }
         this.ItemNodes[i].icon.node.getChildByName('winFlag').active=gameList[i].prize>0;
            
        }
       
    }
   
     onClickClose() {
        this.GameList.active=false
    }


}


